<?php

/**
 * This is the model class for table "{{activity}}".
 *
 * @author luubk
 * @since 14/01/2013 - 21:12:11 
 *
 * The followings are the available columns in table '{{activity}}':
 * @property string $id
 * @property string $object_id
 * @property string $object_type
 * @property string $object_name
 * @property string $type
 * @property integer $created_at
 * @property string $description
 * @property string $actor_type
 * @property integer $actor_id
 * @property string $ip
 * @property string $url
 */
class Activity extends CActiveRecord
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Activity the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{activity}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('created_at, actor_id', 'numerical', 'integerOnly' => true),
            array('object_id, object_type, ip', 'length', 'max' => 30),
            array('object_name, description', 'length', 'max' => 200),
            array('type', 'length', 'max' => 20),
            array('actor_type', 'length', 'max' => 10),
            array('url', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, object_id, object_type, object_name, type, created_at, description, actor_type, actor_id, ip, url', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'admin' => array(self::BELONGS_TO, 'Admin', 'actor_id'),
            'user' => array(self::BELONGS_TO, 'User', 'actor_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $array = array(
            'id' => 'ID',
            'object_id' => 'Object',
            'object_type' => 'Object Type',
            'object_name' => 'Object Name',
            'type' => 'Type',
            'created_at' => 'Created At',
            'description' => 'Description',
            'actor_type' => 'Actor Type',
            'actor_id' => 'Actor',
            'ip' => 'Ip',
            'url' => 'Url',
        );

        foreach ($array as $key => $val)
        {
            $array[$key] = Yii::t('activity', $val);
        }

        return $array;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('object_id', $this->object_id, true);
        $criteria->compare('object_type', $this->object_type, true);
        $criteria->compare('object_name', $this->object_name, true);
        $criteria->compare('type', $this->type, true);
        $criteria->compare('created_at', $this->created_at);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('actor_type', $this->actor_type, true);
        $criteria->compare('actor_id', $this->actor_id);
        $criteria->compare('ip', $this->ip, true);
        $criteria->compare('url', $this->url, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function getActivityString()
    {
        switch ($this->actor_type)
        {
            case 'user':
                $actor = $this->user;
                break;
            default:
                $actor = $this->admin;
                break;
        }

        $doLabel = $this->type == 'delete' ? 'label-important' : "label-info";
        $do = '<span class="label ' . $doLabel . '">' . Yii::t('activity', $this->type) . '</span>';
        $time = '<span class="p-info">' . date('Y/m/d H:i:s', $this->created_at) . '</span>';
        $objectName = '<strong>' . Yii::t($this->object_type, $this->object_name) . '</strong>';
        $object = $this->type == 'delete' ? $objectName : CHtml::link($objectName, $this->url);
        $activity = Yii::t('activity', '{user} {do} a {object}', array(
                '{do}' => $do,
                '{object}' => $object,
                '{time}' => $time,
                '{ip}' => $this->ip,
                '{url}' => $this->url,
                '{user}' => $actor->username,
        ));
        return $activity;
    }

    public static function getTop($limit = 10)
    {
        $criteria = new CDbCriteria;
        $criteria->limit = $limit;
        $criteria->order = "id DESC";
        return self::model()->findAll($criteria);
    }

    public static function viewAllUrl()
    {
        return Yii::app()->controller->createUrl('adminuser/activity', array(
                'lang' => Yii::app()->language
        ));
    }

}